<?php
namespace App\Logic\ZiNiao\Work;

use App\Logic\ZiNiao\Api\GetUserToken;
use App\Logic\ZiNiao\Api\RefreshUserToken;
use App\Models\ZiNiao\UserToken as UserTokenModel;
class UserToken
{
    public static function exe($userCode){
        $getUserToken = new GetUserToken();
        $data = $getUserToken->exe(['userCode' => $userCode]);
        UserTokenModel::query()->updateOrCreate(
            ['userCode' => $userCode],
            [
                'userAccessToken' => $data->userAccessToken,
                'expiresIn' => $data->expiresIn,
                'userRefreshToken' => $data->userRefreshToken,
                'reExpiresIn' => $data->reExpiresIn,
                'enableAuthApp' => $data->enableAuthApp,
            ]
        );
    }

    public static function refresh($userCode){
        $userTokenModel = UserTokenModel::query()->where('userCode',$userCode)->first();
        if($userTokenModel){
            $refreshUserToken = new RefreshUserToken();
            $data = $refreshUserToken->exe(['userRefreshToken' => $userTokenModel->userRefreshToken]);
            $userTokenModel->userAccessToken = $data->userAccessToken;
            $userTokenModel->expiresIn = $data->expiresIn;
            $userTokenModel->save();
        }
    }

}
